window.onload = function () {
    takeOut();
}
function takeOut() {
    var imgbtn = document.getElementById('logo');
    var mask = document.getElementById('mask');
    var closed = document.getElementById('close');
    var button = document.getElementById('btn');
    var ulDom = document.getElementById('messagelist');
    var numArr = [];
    var mini = NaN;
    var index;
    var emptyText = document.getElementsByClassName('emptytext')[0];

    imgbtn.onclick = function () {
        mask.style.display = "block";
        closed.onclick = function () {
            mask.style.display = "none";
        }
    }
    //打开和关闭游戏窗口
    button.onmouseenter = function () {
        this.style.backgroundPosition = '0' + (-40) + 'px';
        this.onmouseleave = function () {
            this.style.backgroundPosition = '0' + ' 0px';
        }
    }
    button.onmousedown = function () {
        this.style.backgroundPosition = '0' + (-80) + 'px';
        emptyText.style.display = 'none';
        creatNum();
        this.onmouseup = function () {
            this.style.backgroundPosition = '0' + (-40) + 'px';
        }
    }
    //鼠标滑动或按下的交互状态的功能
    Array.prototype.mini = function () {
        var mini = this[0];
        for (var i = 1; i < this.length; i++) {
            if (this[i] < mini) {
                mini = this[i];
            }
        }
        return mini;
    }
    //添加数组原型方法，用于取数组中最小数
    function creatNum() {
        var num = Math.floor(Math.random() * 100);
        if (mini == num) {
            creatNum();
            return false;
        }
        numArr.push(num);//产生最小数并存入数组numArr
        console.log(numArr);
        mini = numArr.mini();//获取数组中的最小值
        console.log(mini);
        if (numArr.length > 10) {
            if (num > mini && index == 0) {
                numArr.splice(1, 1);
            } else {
                numArr.shift();
            }
        }
        index = numArr.indexOf(mini);
        refurbishDom(numArr, index)
    }

    function refurbishDom(arr, index) {
        var listStr = '';
        ulDom.innerHTML = '';
        for (var i = 0; i < arr.length; i++) {
            listStr += '<li>扔出一个' + arr[i] + '</li>';
        }
        ulDom.innerHTML = listStr;
        ulDom.getElementsByTagName('li')[index].className = 'selected';
    }
}
